package p07_connections;

import Utils.UtilUtils;
import Utils.db.DbcpUtils;
import Utils.db.JdbcUtilsStatic;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.SQLException;

public class DBCP {
    /**
     * 方法一：硬编码
     *
     * @throws SQLException
     */
    @Test
    public void test01() throws SQLException {
        BasicDataSource source = new BasicDataSource();
        source.setDriverClassName("com.mysql.jdbc.Driver");
        source.setUrl("jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true");
        source.setUsername("root");
        source.setPassword("ys19931111");
        source.setInitialSize(10);
        Connection conn = source.getConnection();
        System.out.println(conn);
        JdbcUtilsStatic.close(conn);
    }

    /**
     * 方法二：配置文件
     */
    @Test
    public void test02() throws Exception {
        BasicDataSource source = BasicDataSourceFactory.createDataSource(UtilUtils.loadProperties("dbcp.properties"));
        Connection conn = source.getConnection();
        System.out.println(conn);
    }
    
    @Test
    public void test03() throws Exception {
        Connection conn = DbcpUtils.getInstance("jdbc.properties").getConnection();
        System.out.println(conn);
        JdbcUtilsStatic.close(conn);
    }
}
